// index.js
const app = getApp()

Page({
  data: {
    tabList: [ {
      title: '待付款',
      id: 1,
    }, {
      title: '进行中',
      id: 2,
    }, {
      title: '已完成',
      id: 3,
    }, {
      title: '已取消',
      id: 0,
    }],
    currentId: 2,

    // 倒计时
    days: 0,
    hours: 0,
    minutes: 0,
    seconds: 0,
    timestamp: 0, // 目标时间戳（以秒为单位）
    child_ids:'',

    btn:1,
    userInfo: {},
    isloading:false,
    type:0, //类型
    lists:[],//列表

    page:1, //分页
    tsmsg:false, //底部提示信息是否显示
    bottommsg:"加载中..."
  },

  onLoad(options) {
    // 假设目标时间戳是当前时间 + 10 天
    const targetTimestamp = Math.floor(Date.now() / 1000) + 7 * 24 * 60 * 60;
    this.setData({
      timestamp: targetTimestamp,
    });

    if(options.i){
      app.globalData.order_type = options.i
    }
    this.setData({
      type:app.globalData.order_type,currentId:app.globalData.order_type
    })
    
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    if (app.globalData.userInfo==undefined || JSON.stringify(app.globalData.userInfo)=='{}') {
      app.userInfoReadyCallback = res => {
        that.setData({
          userInfo:app.globalData.userInfo
        });
        //that.loadready(); //正式
        wx.hideLoading(); //演示
      }
    }else{
      that.setData({
        userInfo:app.globalData.userInfo
      });
      //that.loadready(); //正式
      wx.hideLoading(); //演示
    }
  },

  onShow:function(){
    wx.showShareMenu({
      withShareTicket:true,
      menus:['shareAppMessage','shareTimeline']
    })
    this.setData({
      userInfo:app.globalData.userInfo
    });
    this.loadready();
  },
  loadready:function(){
    var that = this;
    that.setData({
      lists: []
    })
    wx.request({
      url: app.globalData.httpUrl+'/api/device/getOrderList',
      data:{openid:app.globalData.userInfo.openid,user_id:app.globalData.userInfo.id,type:that.data.type,page:1},
      header: {
        'content-type': 'application/x-www-form-urlencoded','appid':app.globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      success (res) { 
          that.setData({
            lists: res.data.list,
            page:1,
            bottommsg: "加载中...",
            tsmsg:false
          });
          wx.hideLoading();
          wx.stopPullDownRefresh();
      },
      error(err) {
        console.log(err);
      }
    });
  },

  //取消订单
  cancel(option){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要取消吗？',
      success: function (sm) {
          if (sm.confirm) {
            wx.request({
              url: app.globalData.httpUrl+'/api/device/cancel_order',
              data:{id:option.currentTarget.dataset.id},
              header: {
                'content-type': 'application/x-www-form-urlencoded','appid':app.globalData.appId
              },
              method: 'POST',
              dataType: 'json',
              success (res) { console.log(res);
                wx.showToast({
                  title: '取消成功',
                })
                setTimeout(function(){
                  that.setData({
                    currentId: 0
                  })
                  app.globalData.order_type = 0
                  that.setData({type: 0});
                  that.loadready(); //重新加载页面
                },2000)
                
                wx.hideLoading();
              }
            })
          }
      }  
    })
  },

  


  changetype:function(option){
    var that = this;
    var type = option.currentTarget.id
    app.globalData.order_type = type
    that.setData({type: type});
    wx.showLoading({
        title: '加载中...',
    })
    that.loadready(); //重新加载页面
    wx.hideLoading();
  },
  //下拉刷新
  onPullDownRefresh:function(){
    this.loadready();
    wx.stopPullDownRefresh();
  },
  //监听触底
  onReachBottom:function(){ 
    var that = this;
    var page = that.data.page+1;
    var list = that.data.lists;
    if(!that.data.tsmsg){ 
      that.setData({
        bottommsg: "加载中...",
        tsmsg:true
      });
      wx.request({
        url: app.globalData.httpUrl+'/api/device/getOrderList',
        data:{openid:app.globalData.userInfo.openid,user_id:app.globalData.userInfo.id,type:that.data.type,page:page},
        header: {
          'content-type': 'application/x-www-form-urlencoded','appid':app.globalData.appId
        },
        method: 'POST',
        dataType: 'json',
        success (res) { 
            if(res.data.num>0){
              that.setData({
                page:page,
                lists: list.concat(res.data.list),
                tsmsg:false
              });
            }else{
              that.setData({
                bottommsg: "没有更多了",
              });
            }
            wx.hideLoading(); //隐藏加载
            wx.stopPullDownRefresh(); //停止下拉刷新
          
        },
        error(err) {
          console.log(err);
        }
      });
    }
  },


  

  //查看多图
  previewImage: function(e) {
    this.setData({
      urls:[e.currentTarget.dataset.id]
    })
    wx.previewImage({
        urls: this.data.urls
    });
  },


  //pay
  pay:function(e){
    var that = this;
    var order_id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '您确定进行支付么？',
      success: function(res) {
        if (res.confirm) {
          wx.hideLoading();           

          wx.request({
            url: app.globalData.httpUrl+"api/device/pay",
            cachetime: "0",
            header: {
                'content-type': 'application/x-www-form-urlencoded','appid':app.globalData.appId
            },
            method: 'POST',
            dataType: 'json',
            data: {
              order_id:order_id,
              openid: app.globalData.userInfo.openid,
              user_id: app.globalData.userInfo.id
            },
            success: function(t) {
              if(t.data.code==200){
                wx.showModal({
                  content: '支付成功',
                })
                setTimeout(function(){
                  that.setData({
                    currentId: 2
                  })
                  app.globalData.order_type = 2
                  that.setData({type: 2});
                  that.loadready(); //重新加载页面
                },2000)
              }else{
                wx.showModal({
                  content: t.data.msg,
                })
              }
              wx.hideLoading();           
            }
          })
        }
      }
    })  
  },

  report(e){
    wx.navigateTo({
      url: '/pages/report/index?order_id='+e.currentTarget.dataset.id,
    })
  },



  onTap(e) {
    var that = this
    this.setData({
      currentId: e.currentTarget.dataset.id
    })
    var type = e.currentTarget.dataset.id
    app.globalData.order_type = e.currentTarget.dataset.id
    that.setData({type: type});
    wx.showLoading({
        title: '加载中...',
    })
    that.loadready(); //重新加载页面
    wx.hideLoading();
    console.log('点击事件')
  },
  
})